public class MyQueueDemo {

    class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val=val;
        }
    }

    public Node head;
    public Node last;
    public int usedSize;

    public void offer(int val){
        Node node=new Node(val);
        if(head==null){
            head=node;
            last=node;
        }
        else{
            last.next=node;
            last=node;
        }
        this.usedSize++;
    }

    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        int val=head.val;
        head=head.next;
        if(head==null){//处理只有一个元素的情况
            last=null;
        }
        this.usedSize--;
        return val;
    }

    public int size() {
        return usedSize;
    }

    public boolean isEmpty(){
        return this.usedSize==0;
    }

    public int peek() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空！");
        }
        return head.val;
    }
}
